package cn.agiledata.bank.interest.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.agiledata.bank.hibernateBean.BankrollType;
import cn.agiledata.bank.interest.dao.IBankrollTypeDAO;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

/**
 * Data access object (DAO) for domain model class BankrollType.
 * @see cn.agiledata.bank.hibernatebean.BankrollType
 * @author MyEclipse - Hibernate Tools
 */
public class BankrollTypeDAOImpl extends IPBHibernate3BaseMgr implements IBankrollTypeDAO {

    private static final Log log = LogFactory.getLog(BankrollTypeDAOImpl.class);
    /**
	 *@see cn.agiledata.bank.interest.dao.IBankrollTypeDAO#getList()
	 */
	public List getList()throws DataAccessException,Exception{
		try {
			HibernateTemplate ht = this.getHibernateTemplate();
			
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BankrollType.class); 
			detachedCriteria.add(Restrictions.eq("isValid",(BankrollType.VALID))).addOrder(Order.desc("rateId")); 

			List list = ht.findByCriteria(detachedCriteria);

			return list;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		
	}
	
	 
	/**
	 *@see cn.agiledata.bank.interest.dao.IBankrollTypeDAO#findByrateId(int)
	 */
	public BankrollType findByRateId(Long rateId) {
		if(rateId==null)return null;
		try {
			HibernateTemplate ht = this.getHibernateTemplate();
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BankrollType.class); 
			detachedCriteria.add(Restrictions.eq("rateId",(rateId))); 

			List list = ht.findByCriteria(detachedCriteria,0,1);
			if(list.size()>0){
				return (BankrollType)list.get(0);
			}else{
				return null;
			}
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	 
	/**
	 *@see cn.agiledata.bank.interest.dao.IBankrollTypeDAO#save(cn.agiledata.bank.hibernateBean.BankrollType)
	 */
	 public void save(BankrollType transientInstance) {
	        log.debug("saving BankrollType instance");
	        try {
	            getHibernateTemplate().saveOrUpdate(transientInstance);
	            log.debug("save successful");
	        } catch (RuntimeException re) {
	            log.error("save failed", re);
	            throw re;
	        }
	    }

	public Long getNewRateId()throws Exception{
		try {
			HibernateTemplate ht = this.getHibernateTemplate();
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BankrollType.class); 
			detachedCriteria.setProjection(Projections.max("rateId")); 

			List list = ht.findByCriteria(detachedCriteria,0,1);
			if(list.size()>0){
				Object obj = list.get(0);

				return  new Long(((Long)obj).intValue()+1);
			}else{
				return new Long(1001);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw e;
		}
	}
	public void update(BankrollType node)throws Exception {
		try {
			HibernateTemplate ht = this.getHibernateTemplate();
			ht.saveOrUpdate(node);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void initDao() {
		//do nothing
	}
    
	public void delete(BankrollType persistentInstance) {
        log.debug("deleting BankrollType instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public BankrollType findById( java.lang.Long id) {
        log.debug("getting BankrollType instance with id: " + id);
        try {
            BankrollType instance = (BankrollType) getHibernateTemplate()
                    .get("cn.agiledata.bank.hibernatebean.BankrollType", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(BankrollType instance) {
        log.debug("finding BankrollType instance by example");
        try {
            List results = getSession()
                    .createCriteria("cn.agiledata.bank.hibernatebean.BankrollType")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public BankrollType merge(BankrollType detachedInstance) {
        log.debug("merging BankrollType instance");
        try {
            BankrollType result = (BankrollType) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(BankrollType instance) {
        log.debug("attaching dirty BankrollType instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(BankrollType instance) {
        log.debug("attaching clean BankrollType instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static BankrollTypeDAOImpl getFromApplicationContext(ApplicationContext ctx) {
    	return (BankrollTypeDAOImpl) ctx.getBean("BankrollTypeDAO");
	}


	public void setInValid(Long rateId) throws Exception {
//		 TODO Auto-generated method stub
		try {
			
			BankrollType bankrollType = this.findByRateId(rateId);
			if(bankrollType==null)return;
			bankrollType.setIsValid(BankrollType.INVALID);
			save(bankrollType);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}


	public void setValid(Long rateId) throws Exception {
		// TODO Auto-generated method stub
		try {
			
			BankrollType bankrollType = this.findByRateId(rateId);
			if(bankrollType==null)return;
			bankrollType.setIsValid(BankrollType.VALID);
			save(bankrollType);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
}